package cn.sfa.service.routine;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * @ClassName：AchievementService
 * @Description：业绩报表业务层
 * @author：LiXiang
 * @date：2012-4-1 下午03:53:53
 */
@Service
public class AchievementService {

	Logger log = LoggerFactory.getLogger(AchievementService.class);

	@Autowired
	NamedParameterJdbcTemplate jdbcTemplate;

	/**
	 * @Title: getAgentUser
	 * @author: LiXiang
	 * @Description: 获取代理人等级
	 * @param userId
	 * @return
	 * @throws
	 */
	public String getAgentLevel(String agentId) {
		String str = "";
		String sql = "select c.* from agentbroker a, caseagentlevel c where a.agentlevelcode = c.agentlevelcode and a.agentid=:AGENTID";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("AGENTID", agentId);

		str = jdbcTemplate.queryForObject(sql, paramMap, new RowMapper<String>() {
			public String mapRow(ResultSet rs, int rowNum) throws SQLException {
				return rs.getString("AGENTLEVELNAME");
			}
		});

		return str;
	}

	/**
	 * @Title: getInfo
	 * @author: LiXiang
	 * @Description: 获取个人业绩统计数据信息
	 * @param agentId
	 *            代理ID
	 * @param firWeekStart
	 *            第一周开始日期
	 * @param firWeekEnd
	 *            第一周结束日期
	 * @param secWeekStrat
	 *            第二周开始日期
	 * @param secWeekEnd
	 *            第二周结束日期
	 * @param thrWeekStrat
	 *            第三周开始日期
	 * @param thrWeekEnd
	 *            第三周结束日期
	 * @param lastWeekStrat
	 *            第四周开始日期
	 * @param lastWeekEnd
	 *            第四周结束日期
	 * @return
	 * @throws
	 */
	public List<Map<String, Object>> getInfo(String agentId, String firWeekStart, String firWeekEnd, String secWeekStrat, String secWeekEnd, String thrWeekStrat, String thrWeekEnd, String lastWeekStrat, String lastWeekEnd) {

		String firSql = "select * from table(pkg_sfa_achievement.getAWeekInfo(:AGENTID ,:FIRWEEKSTART,:FIRWEEKEND))";
		String secSql = "select * from table(pkg_sfa_achievement.getAWeekInfo(:AGENTID ,:SECWEEKSTRAT,:SECWEEKEND))";
		String thrSql = "select * from table(pkg_sfa_achievement.getAWeekInfo(:AGENTID ,:THRWEEKSTRAT,:THRWEEKEND))";
		String lastSql = "select * from table(pkg_sfa_achievement.getAWeekInfo(:AGENTID ,:LASTWEEKSTRAT,:LASTWEEKEND))";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("AGENTID", agentId);
		paramMap.put("FIRWEEKSTART", firWeekStart);
		paramMap.put("FIRWEEKEND", firWeekEnd);
		paramMap.put("SECWEEKSTRAT", secWeekStrat);
		paramMap.put("SECWEEKEND", secWeekEnd);
		paramMap.put("THRWEEKSTRAT", thrWeekStrat);
		paramMap.put("THRWEEKEND", thrWeekEnd);
		paramMap.put("LASTWEEKSTRAT", lastWeekStrat);
		paramMap.put("LASTWEEKEND", lastWeekEnd);
		List<Map<String, Object>> firList = jdbcTemplate.queryForList(firSql, paramMap);
		List<Map<String, Object>> secList = jdbcTemplate.queryForList(secSql, paramMap);
		List<Map<String, Object>> thrList = jdbcTemplate.queryForList(thrSql, paramMap);
		List<Map<String, Object>> lastList = jdbcTemplate.queryForList(lastSql, paramMap);
		Map<Integer, List<Map<String, Object>>> tempMap = new HashMap<Integer, List<Map<String, Object>>>();
		tempMap.put(firList.size(), firList);
		tempMap.put(secList.size(), secList);
		tempMap.put(thrList.size(), thrList);
		tempMap.put(lastList.size(), lastList);
		// 取出集合数最大的LIST
		int max = getMax(new int[] { firList.size(), secList.size(), thrList.size(), lastList.size() }, 3);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		// 深度COPY 最大集合的内置MAP(如果不COPY会出现引用错误的现象)
		for (Map<String, Object> lsit2 : tempMap.get(max)) {
			Map<String, Object> list2 = new HashMap<String, Object>();
			list2.putAll(lsit2);
			list.add(list2);
		}
		// 合并各集合成一条数据集
		for (int i = 0; i < list.size(); i++) {
			if (i < firList.size()) {
				list.get(i).put("FIRAPPLICANTNAME", firList.get(i).get("FIRAPPLICANTNAME"));
				list.get(i).put("FIRPROSTANDARDPREM", firList.get(i).get("FIRPROSTANDARDPREM"));
				list.get(i).put("FIRVALUEPREM", firList.get(i).get("FIRVALUEPREM"));
			} else {
				// 置空前三个字段
				list.get(i).put("FIRAPPLICANTNAME", "");
				list.get(i).put("FIRPROSTANDARDPREM", "");
				list.get(i).put("FIRVALUEPREM", "");
			}
			// 第二周的结果
			if (i < secList.size()) {
				list.get(i).put("SECAPPLICANTNAME", secList.get(i).get("FIRAPPLICANTNAME"));
				list.get(i).put("SECPROSTANDARDPREM", secList.get(i).get("FIRPROSTANDARDPREM"));
				list.get(i).put("SECVALUEPREM", secList.get(i).get("FIRVALUEPREM"));
			}
			// 第三周的结果
			if (i < thrList.size()) {
				list.get(i).put("THRAPPLICANTNAME", thrList.get(i).get("FIRAPPLICANTNAME"));
				list.get(i).put("THRPROSTANDARDPREM", thrList.get(i).get("FIRPROSTANDARDPREM"));
				list.get(i).put("THRVALUEPREM", thrList.get(i).get("FIRVALUEPREM"));
			}
			// 第四周的结果
			if (i < lastList.size()) {
				list.get(i).put("FORAPPLICANTNAME", lastList.get(i).get("FIRAPPLICANTNAME"));
				list.get(i).put("FORPROSTANDARDPREM", lastList.get(i).get("FIRPROSTANDARDPREM"));
				list.get(i).put("FORVALUEPREM", lastList.get(i).get("FIRVALUEPREM"));
			}
		}
		return list;
	}

	private int getMax(int[] numbers, int index) {
		if (index == 0)
			return numbers[0];
		else {
			int[] tempnumber = null;
			tempnumber = Arrays.copyOf(numbers, numbers.length - 1);
			return numbers[index] > getMax(tempnumber, index - 1) ? numbers[index] : getMax(tempnumber, index - 1);
		}
	}

	/**
	 * @Title: getFirstWeekData
	 * @author: LiXiang
	 * @Description: 获取某个星期的数据
	 * @param agentId
	 * @param firWeekStart
	 * @param firWeekEnd
	 * @return
	 * @throws
	 */
	public List<Map<String, Object>> getFirstWeekData(String agentId, String firWeekStart, String firWeekEnd) {
		String sql = "select * from table(pkg_sfa_achievement.getFirstWeekData(:AGENTID ,:FIRWEEKSTART,:FIRWEEKEND))";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("AGENTID", agentId);
		paramMap.put("FIRWEEKSTART", firWeekStart);
		paramMap.put("FIRWEEKEND", firWeekEnd);
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, paramMap);
		return list;
	}

	// team

	/**
	 * @Title: getTeamInfo
	 * @author: LiXiang
	 * @Description: TODO
	 * @param agentId
	 *            代理ID
	 * @param firWeekStart
	 *            第一周开始日期
	 * @param firWeekEnd
	 *            第一周结束日期
	 * @param secWeekStrat
	 *            第二周开始日期
	 * @param secWeekEnd
	 *            第二周结束日期
	 * @param thrWeekStrat
	 *            第三周开始日期
	 * @param thrWeekEnd
	 *            第三周结束日期
	 * @param lastWeekStrat
	 *            第四周开始日期
	 * @param lastWeekEnd
	 *            第四周结束日期
	 * @return List<Map<String,Object>>
	 * @throws
	 */
	public List<Map<String, Object>> getTeamInfo(String agentId, String firWeekStart, String firWeekEnd, String secWeekStrat, String secWeekEnd, String thrWeekStrat, String thrWeekEnd, String lastWeekStrat, String lastWeekEnd) {


		List<Map<String, Object>> firList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> secList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> thrList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> lastList = new ArrayList<Map<String, Object>>();

		if(firWeekStart!=null){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			String firSql = "select * from table(pkg_sfa_achievement_team.getAWeekInfo(:AGENTID ,:FIRWEEKSTART,:FIRWEEKEND))";
			paramMap.put("AGENTID", agentId);
			paramMap.put("FIRWEEKSTART", firWeekStart);
			paramMap.put("FIRWEEKEND", firWeekEnd);
			firList = jdbcTemplate.queryForList(firSql, paramMap);
		}
		
		if(secWeekStrat!=null){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			String secSql = "select * from table(pkg_sfa_achievement_team.getAWeekInfo(:AGENTID ,:SECWEEKSTRAT,:SECWEEKEND))";
			paramMap.put("AGENTID", agentId);
			paramMap.put("SECWEEKSTRAT", secWeekStrat);
			paramMap.put("SECWEEKEND", secWeekEnd);
			secList = jdbcTemplate.queryForList(secSql, paramMap);
		}
		
		if(thrWeekStrat!=null){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			String thrSql = "select * from table(pkg_sfa_achievement_team.getAWeekInfo(:AGENTID ,:THRWEEKSTRAT,:THRWEEKEND))";
			paramMap.put("AGENTID", agentId);
			paramMap.put("THRWEEKSTRAT", thrWeekStrat);
			paramMap.put("THRWEEKEND", thrWeekEnd);
			thrList = jdbcTemplate.queryForList(thrSql, paramMap);
		}
		
		if(lastWeekStrat!=null){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			String lastSql = "select * from table(pkg_sfa_achievement_team.getAWeekInfo(:AGENTID ,:LASTWEEKSTRAT,:LASTWEEKEND))";
			paramMap.put("AGENTID", agentId);
			paramMap.put("LASTWEEKSTRAT", lastWeekStrat);
			paramMap.put("LASTWEEKEND", lastWeekEnd);
			lastList = jdbcTemplate.queryForList(lastSql, paramMap);
		}



		Map<Integer, List<Map<String, Object>>> tempMap = new HashMap<Integer, List<Map<String, Object>>>();
		tempMap.put(firList.size(), firList);
		tempMap.put(secList.size(), secList);
		tempMap.put(thrList.size(), thrList);
		tempMap.put(lastList.size(), lastList);
		// 取出集合数最大的LIST
		int max = getMax(new int[] { firList.size(), secList.size(), thrList.size(), lastList.size() }, 3);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		// 深度COPY 最大集合的内置MAP(如果不COPY会出现引用错误的现象)
		for (Map<String, Object> lsit2 : tempMap.get(max)) {
			Map<String, Object> list2 = new HashMap<String, Object>();
			list2.putAll(lsit2);
			list.add(list2);
		}
		// 合并各集合成一条数据集
		for (int i = 0; i < list.size(); i++) {
			if (i < firList.size()) {
				list.get(i).put("FIRAPPLICANTNAME", firList.get(i).get("FIRAPPLICANTNAME"));
				list.get(i).put("FIRPROSTANDARDPREM", firList.get(i).get("FIRPROSTANDARDPREM"));
				list.get(i).put("FIRVALUEPREM", firList.get(i).get("FIRVALUEPREM"));
			} else {
				// 置空前三个字段
				list.get(i).put("FIRAPPLICANTNAME", "");
				list.get(i).put("FIRPROSTANDARDPREM", "");
				list.get(i).put("FIRVALUEPREM", "");
			}
			// 第二周的结果
			if (i < secList.size()) {
				list.get(i).put("SECAPPLICANTNAME", secList.get(i).get("FIRAPPLICANTNAME"));
				list.get(i).put("SECPROSTANDARDPREM", secList.get(i).get("FIRPROSTANDARDPREM"));
				list.get(i).put("SECVALUEPREM", secList.get(i).get("FIRVALUEPREM"));
			}
			// 第三周的结果
			if (i < thrList.size()) {
				list.get(i).put("THRAPPLICANTNAME", thrList.get(i).get("FIRAPPLICANTNAME"));
				list.get(i).put("THRPROSTANDARDPREM", thrList.get(i).get("FIRPROSTANDARDPREM"));
				list.get(i).put("THRVALUEPREM", thrList.get(i).get("FIRVALUEPREM"));
			}
			// 第四周的结果
			if (i < lastList.size()) {
				list.get(i).put("FORAPPLICANTNAME", lastList.get(i).get("FIRAPPLICANTNAME"));
				list.get(i).put("FORPROSTANDARDPREM", lastList.get(i).get("FIRPROSTANDARDPREM"));
				list.get(i).put("FORVALUEPREM", lastList.get(i).get("FIRVALUEPREM"));
			}
		}
		return list;
	}

	/**
	 * @Title: getWeekDataTeam
	 * @author: LiXiang
	 * @Description: 获取团队中一个星期的数据
	 * @param agentId
	 *            代理ID
	 * @param weekStart
	 *            开始日期
	 * @param weekEnd
	 *            结束日期
	 * @return
	 * @throws
	 */
	public List<Map<String, Object>> getWeekDataTeam(String agentId, String weekStart, String weekEnd) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if(weekStart!=null&&!"".equals(weekStart)){
			String sql = "select * from table(pkg_sfa_achievement_team.getFirstWeekData(:AGENTID ,:FIRWEEKSTART,:FIRWEEKEND))";
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("AGENTID", agentId);
			paramMap.put("FIRWEEKSTART", weekStart);
			paramMap.put("FIRWEEKEND", weekEnd);
			list = jdbcTemplate.queryForList(sql, paramMap);
		}
		return list;
	}

	/**
	 * @Title: getZXC
	 * @author: LiXiang
	 * @Description: 获取直辖处成员数量
	 * @param agentId
	 *            代理ID
	 * @param weekStart
	 *            开始日期
	 * @param weekEnd
	 *            结束日期
	 * @return
	 * @throws
	 */
	public int getZXC(String agentId) {

		String sql = "select count(agentid) from table(pkg_sfa_achievement_team.getzxc(:AGENTID))";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("AGENTID", agentId);
		int i = jdbcTemplate.queryForInt(sql, paramMap);
		return i;
	}

	/**
	 * @Title: getAWeekDataPolicy
	 * @author: LiXiang
	 * @Description: 获取某星期回单数量
	 * @param agentId
	 *            代理ID
	 * @param weekStart
	 *            开始日期
	 * @param weekEnd
	 *            结束日期
	 * @return
	 * @throws
	 */
	public int getAWeekDataPolicy(String agentId, String firWeekStart, String firWeekEnd) {
		int count = 0;
		if(firWeekStart!=null&&!"".equals(firWeekStart)){
			String policyCount = "";
			String sql = "select * from table(pkg_sfa_achievement_team.getAWeekDataPolicy(:AGENTID ,:FIRWEEKSTART,:FIRWEEKEND))";
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("AGENTID", agentId);
			paramMap.put("FIRWEEKSTART", firWeekStart);
			paramMap.put("FIRWEEKEND", firWeekEnd);
			List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, paramMap);
			for (Map<String, Object> policy : list) {
				policyCount = (String) policy.get("FIRVALUEPREM");
			}
			if (policyCount != null && !"".equals(policyCount))
				count =  Integer.valueOf(policyCount);
			else
				count =  0;
		}
		return count;
			
	}
}
